home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1994 March / Internet Info CD-ROM (Walnut Creek) (March 1994).iso / networking / applic / ntp / acts.zoo / parset.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-10-15  |  6.2 KB  |  207 lines

  1. void parset(buf)
  2. char buf[280];
  3. {
  4. #include "nbstime.h"
  5. #include <stdio.h>
  6. #ifdef IBMPC
  7. #include <dos.h>
  8. #if defined(MSC)
  9. struct dosdate_t date;
  10. struct dostime_t time;
  11. #endif
  12. #endif
  13. char c;
  14. int j,yr,mo,day,hr,min,sec,is,dst;
  15. #ifdef SUN
  16. #include <sys/time.h>
  17. #endif
  18. #ifdef IBMPC
  19. extern int utcdif;           /* local time - utc in hours */
  20. extern int dsflag;           /* daylight saving time? 1=yes, 0=no */
  21. extern int atflag;           /* AT-type machine? 1=yes, 0=no */
  22. static int lday[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31}; /*last day of month*/
  23. char cnvbcd();
  24. char centc,yrc,moc,dayc,hrc,minc,secc;
  25. int dsdone = 0;
  26. #endif
  27. #ifdef SUN
  28. long int mjd;
  29. long int mjd0 = 40587;     /* mjd of 1/1/70 -- origin of SUN time */
  30. struct timeval tvv,*tp;
  31. #endif
  32. /*
  33.         this subroutine receives a time string in character
  34.         array buf.  it is parsed and used to set the system
  35.         clock.  the origin of the parser is the - character between
  36.         the year and the month so that leading stuff will simply be
  37.         ignored.
  38.  
  39.         this subroutine uses global variables dsflag to check for
  40.         daylight savings time
  41.  
  42.     for the IBMPC version, the subroutine converts to local
  43.     time if requested by the configuration file parameters
  44.     (including a conversion to daylight savings time if needed.
  45.     the SUN version sets the time directly in UTC by converting
  46.     the received time to seconds since 1/1/70.  to simplify
  47.     this conversion, the mjd, rather than the year-month-day
  48.     is used.
  49. */
  50.         for(j=0; (buf[j] != 0) && (buf[j] != '-') ; j++) ;
  51.         sscanf(&buf[j-2],"%2d-%2d-%2d %2d:%2d:%2d %d",&yr,&mo,&day,
  52.         &hr,&min,&sec,&dst);
  53. #ifdef SUN
  54.     sscanf(&buf[j-8],"%5ld",&mjd);
  55. /*
  56.     convert received day number to seconds since 1/1/70 and
  57.     add in hour minute and second.
  58. */
  59.     tp= &tvv;
  60.     mjd= 86400*(mjd - mjd0);
  61.     mjd= mjd + 3600*hr + 60*min + sec;
  62.     tp->tv_sec=mjd;
  63.     tp->tv_usec=0;
  64.     settimeofday(tp,0);
  65. #endif
  66. #ifdef IBMPC
  67. /*     
  68.     if IBMPC version is being generated, we must now convert
  69.     to local time (including daylight savings time as needed
  70. */
  71. /*
  72.         make standard-time portion of dst flag contiguous
  73. */
  74.         if(dst == 0) dst = 100;
  75. /*
  76.         convert from utc to local time. note that although minute
  77.         and second are already correct, there is no BIOS call to
  78.         set just those parameters so we have to wait until the hour
  79.         is corrected and then set them all at once.
  80.  
  81.         note that daylight savings time flag must also be updated
  82.         if conversion to local time changes the day
  83.  
  84.         **      following correction is for version of 12 May 88:
  85.         **      this correction compiled for final testing 31 may
  86.         daylight savings time correction is done in two parts:
  87.  
  88.         if we are solidly in daylight savings time, then do correction
  89.         immediately since it may change the day, and therefore it must
  90.         be done before the check for hour/day overflow.
  91.  
  92.         if this is a transition day, then correction cannot be done now
  93.         since the hour/day overflow may change the daylight savings flag
  94.         dst thereby changing whether or not the correction is needed.
  95.         thus correction must be postponed.
  96.  
  97.         parameter dsdone makes sure that the correction is not done twice
  98.         if we are  moved into a transition day by the hour/day overflow */
  99.         if( (yr & 3) == 0 ) lday[2]=29;  /* 29 days for Feb in leap year */
  100.         hr += utcdif;                    /* convert hour to local time */
  101.         if( (dsflag !=0) && (dst <= 50) && (dst > 1) )
  102.         {
  103.         hr++;
  104.         dsdone=1;
  105.         }
  106.         if(hr < 0)
  107.         {
  108.         hr += 24;
  109.         day--;
  110.         dst++;         /* update daylight savings flag for change of day*/
  111.         if(day < 1)
  112.            {
  113.            mo--;
  114.            if(mo < 1)
  115.               {
  116.               mo=12;
  117.               yr--;
  118.               }
  119.            day=lday[mo];
  120.            }
  121.         }
  122.         if(hr > 23)
  123.         {
  124.         hr -= 24;
  125.         day++;
  126.         dst--;        /* update daylight saving flag for change of day */                                                                               if(day >
  127.  lday[mo])
  128.            {
  129.            day=1;
  130.            mo++;
  131.            if(mo > 12)
  132.               {
  133.               mo=1;
  134.               yr++;
  135.               }
  136.            }
  137.         }
  138. /*
  139.         now finish up daylight savings time if enabled
  140.         only thing left to do is to deal with the transition days
  141.         flag dsdone makes sure that transition day is not done twice
  142. */
  143.         if( (dsflag != 0) && (dsdone == 0) )
  144.         {
  145.            if( (dst == 51) && (hr >= 2) ) hr++;
  146.            if( (dst ==  1) && (hr <  2) ) hr++;
  147.         }
  148. #if defined(MSC)
  149.     time.hour=hr;
  150.     time.second=sec;
  151.     time.minute=min;
  152.     time.hsecond=10;
  153.     _dos_settime(&time);
  154.         yr += 1900;       /* 20-th century assumed here */
  155.     date.year=yr;
  156.     date.month=mo;
  157.     date.day=day;
  158.     _dos_setdate(&date);
  159. #else
  160.         _AH=0x2d;
  161.         _CH=hr;
  162.         _DH=sec;
  163.         _CL=min;
  164.         _DL=10;       /* approximate machine-dependent latency correctio
  165. n*/
  166.         geninterrupt(0x21);
  167.         yr += 1900;       /* 20-th century assumed here */
  168.         _AH=0x2b;
  169.         _CX=yr;
  170.         _DH=mo;
  171.         _DL=day;
  172.         geninterrupt(0x21);
  173. #endif
  174.         printf("\n Computer clock set.");
  175. /*
  176.         code to set AT CMOS clock. note that CMOS clock requires
  177.         time to be converted to BCD digits, packed two to a byte. this
  178.         is done using routine cnvbcd.
  179. */
  180. #if defined(MSC)==0
  181.         if(atflag != 0)
  182.         {
  183.         centc=cnvbcd(19);        /* 20-th century assumed here */
  184.         yrc=cnvbcd(yr-1900);     /* note that 20-th century assumed */
  185.         moc=cnvbcd(mo);
  186.         dayc=cnvbcd(day);
  187.         hrc=cnvbcd(hr);
  188.         minc=cnvbcd(min);
  189.         secc=cnvbcd(sec);
  190.         _AH=3;
  191.         _CH=hrc;
  192.         _CL=minc;
  193.         _DH=secc;
  194.         _DL=dsflag;             /* copy daylight savings to CMOS clock */
  195.         geninterrupt(0x1a);
  196.         _AH=5;
  197.         _CH=centc;
  198.         _CL=yrc;
  199.         _DH=moc;
  200.         _DL=dayc;
  201.         geninterrupt(0x1a);
  202.         printf("\n CMOS clock set.");
  203.         }
  204. #endif
  205. #endif
  206. }
  207.